package level4.engines
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLRequest;
	
	import level4.grid_case.grid.Grid;
	import level4.grid_case.grid.Node;
	import level4.interfaces.dispose.IDisposable;
	/**
	 * 地图拼接实现,小块 序号从0开始,如 xx0_0,xx1_0
	 * */
	public class MapEngine implements IDisposable
	{
		//地图网格
		private var mapGrid:Grid;
		//单元格的 大小
		private var cellWidth:int;
		private var cellHeight:int;
		//位图的url
		private var url:String;
		//新加入的位图
		private var newNodes:Array = [];
		
		public function MapEngine()
		{
			
		}
		
		public function setup(numRows:int,numCols:int,cellWidth:int,cellHeight:int,url:String):void{
			dispose();
			mapGrid = new Grid(numCols,numRows);
			
			this.cellHeight = cellHeight;
			this.cellWidth = cellWidth;
			this.url = url;
		}
		
		public function dispose():void{
			if(mapGrid)
				mapGrid.dispose();
		}
		
		/**
		 * 随滚动背景,开始替换工作
		 * */
		public function startWork(bg:DisplayObjectContainer):void{
			var st:Stage = bg.stage;
			var node:Node = null;
			//视口,坐标转入 目标坐标系
			var tlPt:Point = bg.globalToLocal(new Point());
			var brPt:Point = bg.globalToLocal(new Point(st.stageWidth,st.stageHeight));
			//获取,4顶点的网格坐标
			var startGridCoordX:int = tlPt.x / cellWidth;
			var endGridCoordX :int = brPt.x / cellWidth;
			var startGridCoordY:int = tlPt.y / cellHeight;
			var endGridCoordY:int = brPt.y / cellHeight;
			
			//遍历grid,
			for(var i:int = startGridCoordY;i<=endGridCoordY;i++){	//行
				for(var j:int = startGridCoordX;j<=endGridCoordX;j++){	//列
					if(mapGrid.checkInBounds(j,i)){
						node = mapGrid.getNode(j,i);
						
						if(node){
							loadPng(node,bg);
							newNodes.push(node);
						}
					}
				}
			}
			
		}
		/**
		 * 销毁 新加入的位图数据
		 * */
		private function disposeThem():void{
			var node:Node = null;
			for each(node in newNodes){
				if(node && node.tag){
					(node.tag as Bitmap).bitmapData.dispose();
				}
			}
			newNodes.length = 0;
		}
		
		
		private function loadPng(node:Node,bg:DisplayObjectContainer):void{
			
			if(node && node.tag){
				//不处理
			}else{
				var l:Loader = new Loader();
					
					l.load(new URLRequest(url+node.y+"_"+node.x+".png"));
					l.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadComplete);
			}
			
			////////////////
			//闭包
			////////////////
			function onLoadComplete(e:Event):void{
				var bmd:BitmapData = new BitmapData(cellWidth,cellHeight,true,0);
					bmd.lock();
					bmd.draw(l);
					l.unloadAndStop(true);
					bmd.unlock();
				var bmp:Bitmap = node.tag as Bitmap;
				if(!bmp){
					bmp = new Bitmap(bmd);
				}
				
				bmp.x = node.x * cellWidth;
				bmp.y = node.y * cellHeight;
				bmp.bitmapData = bmd;
					
				bg.addChild(bmp);
					node.tag = bmp;
			}
		}
		
	}
}